<?php
/**
 * sitedefinetemplate.php
 * Create on 2015-03-25 10:48:38
 * Create by yuebin
 */
namespace haibao\cms\business\site;
use haibao\cms\business\BusinessException;
use haibao\frame\data\query\Condition;
use haibao\cms\data\site\SiteChannelTemplate as SiteChannelTemplateData;
use haibao\cms\model\filter\site\SiteChannelTemplate as SiteChannelTemplateFilter;
use haibao\cms\model\filter\site\SiteChannel as SiteChannelFilter;
use haibao\cms\model\filter\site\PageTemplate as PageTemplateFilter;
use haibao\cms\model\filter\site\SiteChannelTag as SiteChannelTagFilter;
use haibao\cms\business\PageControl as PageControlBusiness;

class SiteChannelTemplate extends \haibao\cms\business\Base{

	/**
	 * 添加站点频道
	 * @param : Object \haibao\cms\model\data\site\SiteChannelTemplate $siteChannelTemplateModel
	 */
	public static function addSiteChannelTemplate($siteChannelTemplateModel){
	
		self::checkOperation(self::FUNC_CODE_SITE_CHANNEL_TEMPLATE_ADD);
	
		self::checkModelData($siteChannelTemplateModel);

		$siteChannelTemplateData = new SiteChannelTemplateData();
		$siteChannelTemplateData->addSiteChannelTemplate($siteChannelTemplateModel);
	
		self::saveOperateHistory(self::FUNC_CODE_SITE_CHANNEL_ADD, $siteChannelTemplateModel->toArray(true,true));
	
	}
	
	/**
	 * 更新站点频道
	 * @param : Object \haibao\cms\model\data\site\SiteChannelTemplate $siteChannelTemplateModel
	 */
	public static function updateSiteChannelTmpById($siteChannelTmpModel){

		self::checkOperation(self::FUNC_CODE_SITE_CHANNEL_TEMPLATE_EDIT);

		self::checkModelData($siteChannelTmpModel);
		
		$siteChannelTmpData = new SiteChannelTemplateData();
		$siteChannelTmpData->updateSiteChannelTmpById($siteChannelTmpModel);
	
		self::saveOperateHistory(self::FUNC_CODE_SITE_CHANNEL_TEMPLATE_EDIT, $siteChannelTmpModel->toArray(true,true));
	
	}	
	
	/**
	 * 获取频道模版信息
	 * @param : Object $filter
	 * @param : Int $isOne 是否取一条
	 */
	public static function getChannelTemplateByFilter($filter,$isOne = true){
	
		$data = new SiteChannelTemplateData();
		if($isOne){
			$channelTemplate = $data->getOne($filter);
		}else{
	
			$channelTemplate = $data->getAll($filter);
		}
		return $channelTemplate;
	}
	
	/**
	 * 删除频道模版
	 * @param : Int $id
	 * @param : Int $status
	 */
	public static function deleteChannelTmpById($id, $status = \haibao\cms\model\data\site\SiteChannelTemplate::STATUS_ENABLE){

		self::checkOperation(self::FUNC_CODE_SITE_CHANNEL_TEMPLATE_DEL);
		$data = new SiteChannelTemplateData();
		$data->deleteChannelTmpById($id,$status);
		self::saveOperateHistory(self::FUNC_CODE_SITE_CHANNEL_TEMPLATE_DEL, array($id,$status));
	}
	
	/**
	 * 根据标签ID获取站点频道关联关系
	 */
	public static function getSiteChannelTmpByTagId($tagId,$isPc=true){
		
		$id = 0;
		$siteChannelTemplateFilter = new SiteChannelTemplateFilter();
		$siteChannelTemplateFilter->select(array(
				SiteChannelTemplateFilter::CONDITION_FIELD_NAME_ID,
		));
		$siteChannelTagFilter = new SiteChannelTagFilter();
		$siteChannelTemplateFilter->leftJoin($siteChannelTagFilter,null,SiteChannelTemplateFilter::CONDITION_FIELD_NAME_CHANNEL_ID,SiteChannelTagFilter::CONDITION_FIELD_NAME_CHANNEL_ID,SiteChannelTemplateFilter::CONDITION_FIELD_NAME_SITE_CHANNEL_TAG,false);
		$siteChannelFilter = new SiteChannelFilter();
		$siteChannelTemplateFilter->leftJoin($siteChannelFilter,null,SiteChannelTemplateFilter::CONDITION_FIELD_NAME_CHANNEL_ID,SiteChannelFilter::CONDITION_FIELD_NAME_ID,SiteChannelTemplateFilter::CONDITION_FIELD_NAME_CHANNEL_INFO,false);
		
		if($isPc){
			$siteChannelFilter->where(SiteChannelFilter::CONDITION_FIELD_NAME_SITE_ID,Condition::CONDITION_EQUAL,\haibao\cms\model\data\site\SiteDefine::PC_SITE_ID);
		}else{
			$siteChannelFilter->where(SiteChannelFilter::CONDITION_FIELD_NAME_SITE_ID,Condition::CONDITION_EQUAL,\haibao\cms\model\data\site\SiteDefine::WAP_SITE_ID);
		}
		$siteChannelTemplateFilter->where(SiteChannelTemplateFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\site\SiteChannelTemplate::STATUS_ENABLE);
		$siteChannelTagFilter->where(SiteChannelTagFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_EQUAL,(int)$tagId);
		$data = new SiteChannelTemplateData();
		$channelTemplate = $data->getOne($siteChannelTemplateFilter);
		if($channelTemplate){
			$id = $channelTemplate->Id;
		}
		return $id;
	}
	
	/**
	 * 更新上次修改时间为当前时间
	 */
	public static function updateLastBuildTime($id){

		$data = new SiteChannelTemplateData();
		$data->updateLastBuildTime($id);
		
	}
	/**
	 * 校验实体数据
	 */
	private static function checkModelData($siteChannelTemplateModel){
		
		if(!$siteChannelTemplateModel->ChannelId){
			throw new BusinessException('频道ID不能为空');
		}
		if(!$siteChannelTemplateModel->TemplateId){
			throw new BusinessException('模版ID不能为空');
		}

		if(!$siteChannelTemplateModel->FileName){
			throw new BusinessException('文件名称不能为空');
		}

		if($siteChannelTemplateModel->AutoBuild == \haibao\cms\model\data\site\SiteChannelTemplate::STATUS_ENABLE){
			if(!$siteChannelTemplateModel->BuildCycle){
				throw new BusinessException('静态文件生成周期参数错误');
			}
		}
	}
	

}
